#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QClipboard>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_edt_title_editingFinished()
{
    string inputTitle = ui->edt_title->text().toStdString();
    this->blogHeader.setBlogTitle(inputTitle);

    // get current data
    string dataTime = "";
    CShared::getCurrentTime(dataTime);
    this->blogHeader.setBlogDate(dataTime);

    // update ui
    ui->edt_data->setText(QString::fromLocal8Bit(dataTime.data()));
    update_outBlogHeader();
}

void MainWindow::update_outBlogHeader()
{
    ui->edt_outBlogHeader->setText(QString::fromLocal8Bit(this->blogHeader.getFormatBlogHeader().data()));
}

// user costum data
void MainWindow::on_edt_data_editingFinished()
{
    string userInputData = ui->edt_data->text().toStdString();
    this->blogHeader.setBlogDate(userInputData);

    update_outBlogHeader();
}

void MainWindow::on_edt_categories_editingFinished()
{
    string categories = ui->edt_categories->text().toStdString();
    this->blogHeader.setBlogCategories(categories);
    update_outBlogHeader();
}

void MainWindow::on_edt_tags_editingFinished()
{
    string tags = ui->edt_tags->text().toStdString();
    this->blogHeader.setBlogTags(tags);
    update_outBlogHeader();
}

void MainWindow::on_cbx_swiper_currentIndexChanged(int index)
{
    if  (!index)
    {
        this->blogHeader.setIsSwiper(false);
    }
    else
    {
        this->blogHeader.setIsSwiper(true);
    }
    update_outBlogHeader();
}

void MainWindow::on_edt_swiperImg_editingFinished()
{
    string swiperImg = ui->edt_swiperImg->text().toStdString();
    this->blogHeader.setSwiperImgURL(swiperImg);
    update_outBlogHeader();
}

void MainWindow::on_btn_2picbedlink_clicked()
{
    string swiperImg = ui->edt_swiperImg->text().toStdString();
    swiperImg = CShared::picbed + swiperImg;
    this->blogHeader.setSwiperImgURL(swiperImg);

    // update ui
    ui->edt_swiperImg->setText(QString::fromLocal8Bit(swiperImg.data()));
    update_outBlogHeader();
}

void MainWindow::on_cbx_top_currentIndexChanged(int index)
{
    if  (!index)
    {
        this->blogHeader.setIsTop(false);
    }
    else
    {
        this->blogHeader.setIsTop(true);
    }
    update_outBlogHeader();
}

void MainWindow::on_cbx_toc_currentIndexChanged(int index)
{
    if  (!index)
    {
        // show toc
        this->blogHeader.setIsToc(true);
    }
    else
    {
        this->blogHeader.setIsToc(false);
    }
    update_outBlogHeader();
}

void MainWindow::on_cbx_tocOpen_currentIndexChanged(int index)
{
    if  (!index)
    {
        // toc open
        this->blogHeader.setIsTocOpen(true);
    }
    else
    {
        this->blogHeader.setIsTocOpen(false);
    }
    update_outBlogHeader();
}

void MainWindow::on_cbx_comments_currentIndexChanged(int index)
{
    if  (!index)
    {
        // toc open
        this->blogHeader.setIsComments(false);
    }
    else
    {
        this->blogHeader.setIsComments(true);
    }
    update_outBlogHeader();
}

void MainWindow::on_edt_img_editingFinished()
{
    string imgURL = ui->edt_img->text().toStdString();
    this->blogHeader.setBlogImgUrl(imgURL);
    update_outBlogHeader();
}

void MainWindow::on_btn2_2picbedlink_clicked()
{
    string img = ui->edt_img->text().toStdString();
    img = CShared::picbed + img;
    this->blogHeader.setBlogImgUrl(img);

    // update ui
    ui->edt_img->setText(QString::fromLocal8Bit(img.data()) );
    update_outBlogHeader();
}

void MainWindow::on_edt_more_cursorPositionChanged()
{
    string moreInfo = ui->edt_more->toPlainText().toStdString();
    this->blogHeader.setMoreText(moreInfo);

    // update ui
    update_outBlogHeader();
}

// copy to clip
void MainWindow::on_btn_copyOutText_clicked()
{
     QApplication::clipboard()->setText(ui->edt_outBlogHeader->toPlainText());
     ui->statusBar->showMessage("已复制", 1000);
}
